<?php


namespace app\api\controller\traits\coupon_order;


use api\channel\jutuike\AroundUnion;
use api\channel\jutuike\FoodUnion;
use api\channel\jutuike\TravelUnion;
use api\channel\jutuike\Union;
use app\model\CouponProduct;
use app\model\CouponProductChannel;

trait Jutuike
{
    /**
     * @descripe 临时方案（为了少点一次提交）
     * v2.3.1 根据品牌跳转到聚推客
     */
    public function getJumpUrlByBrandId()
    {

        $brand_id = input('brand_id');

        $jutuike = new \api\channel\jutuike\Coupon();

        $agent_id = $this->request->agentChannel['agent_id'];
        $user_id = $this->request->user['id'];
        $channel_id = $this->request->agentChannel['id'];

        $coupon = CouponProductChannel::query()
            ->alias('c')
            ->where('c.status', CouponProductChannel::STATUS_ACTIVE)
            ->where('p.status', CouponProduct::STATUS_ACTIVE)
            ->join('coupon_product p', 'p.id = c.product_id')
            ->where('p.brand_id', $brand_id)
            ->find();

        if (!$coupon) {
            return fail('不存在该权益品牌');
        }
        $mobile = input('mobile','');
        $res = $jutuike->productUrl($coupon->origin_id, compact('agent_id', 'user_id','channel_id','mobile'));

        if (empty($res)){
            return fail('打开失败，请稍后再试');
        }

        return success('', $res);

    }

    /**
     * 跳转到聚推客页面  出行和加油
     */
    public function getJumpUrlByTravelId(){


        $type = input('id');
        $jutuike = new TravelUnion();

        $agent_id = $this->request->agentChannel['agent_id'];
        $channel_id = $this->request->agentChannel['id'];
        $user_id = $this->request->user['id'];
        $mobile = input('mobile','');
        $res = $type==99?$jutuike->gaodeUrl(compact('agent_id','user_id','channel_id','mobile')):$jutuike->productUrl($type,compact('agent_id','user_id','channel_id','mobile'));

        return success('操作成功',$res);
    }

    /**
     * 获取跳转聚推客餐饮的链接url
     */
    public function getFoodJumpUrl(){

        $product_type = input('product_type','kfc');

        $service = new FoodUnion($product_type);

        $agent_id = $this->request->agentChannel['agent_id'];
        $channel_id = $this->request->agentChannel['id'];
        $user_id = $this->request->user['id'];
        $mobile = input('mobile','');
        $res = $service->productUrl(compact('agent_id','user_id','channel_id','mobile'));

        return success('',$res);
    }

    /**
     * 获取聚推客统一转化链接 v2.3.3
     */
    public function getUnionUrl(){

        $service = new Union();

        $act_id  = input('act_id','54');//默认联联周边游

        $agent_id = $this->request->agentChannel['agent_id'];
        $channel_id = $this->request->agentChannel['id'];
        $user_id = $this->request->user['id'];
        $mobile = input('mobile','');
        $res = $service->productUrl(compact('agent_id','user_id','channel_id','mobile'),$act_id);
        return success('',$res);

    }


    /**
     * 周边游 https://www.jutuike.com/doc/100
     * @return \think\response\Json
     */
    public function getAround(){
        $service = new AroundUnion();
        $agent_id = $this->request->agentChannel['agent_id'];
        $channel_id = $this->request->agentChannel['id'];
        $user_id = $this->request->user['id'];
        $mobile = input('mobile','');
        $res = $service->jumpAround(compact('agent_id','user_id','channel_id','mobile'));
        return success('',$res);
    }

}